<template>
  <view>
    <!-- 顶部搜索框 -->
    <!-- 方法1.绑定点击事件用native修饰符 -->
    <!-- 方法2.调用this.$emit() -->
    <!-- <my-search @click.native="goToSearch"></my-search> -->
    <my-search @click="goToSearch"></my-search>
    <view class="scroll-container">
      <!-- 左侧滑动导航区 -->
      <scroll-view class="scroll-left" scroll-y="true" :style="{ height: windowHeight + 'px' }">
        <block v-for="(item,i) in cateList" :key="i">
          <view :class="['lfet-scroll-item',i===active?'active':'']" @click="activeChange(i)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧滑动展示区 -->
      <scroll-view class="scroll-right" scroll-y="true" :style="{ height: windowHeight + 'px' }"
        :scroll-top="scrollTop">
        <view class="cate-level2" v-for="(val,i) in cateLevel2" :key="i">
          <view class="cate-level2-title">{{val.cat_name}}</view>
          <view class="cate-level3">
            <view class="cate-level3-item" v-for="(val1,i1) in val.children" :key="i1" @click="goToGoodsList(val1)">
              <image :src="val1.cat_icon"></image>
              <text>{{val1.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>

    </view>
  </view>
</template>

<script>
  import tabBarBadge from '@/mixins/tabBarBadge.js'
  export default {
    mixins: [tabBarBadge],
    data() {
      return {
        windowHeight: 0,
        cateList: [],
        cateLevel2: [],
        // 激活项
        active: 0,
        scrollTop: 0,
      };
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.windowHeight = sysInfo.windowHeight - 50
      // 获取分类
      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) {
          uni.showMsg('分类数据加载失败...')
        } else {
          this.cateList = res.message
          this.cateLevel2 = res.message[0].children
          // console.log('cateLevel2', this.cateLevel2)
        }
      },
      // 左侧导航切换
      activeChange(i) {
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 0.1 : 0
        // console.log('cateLevel2', this.cateLevel2)
      },
      // 点击至商品详情页
      goToGoodsList(item) {
        uni.navigateTo({
          url: "/subpkg/goods_list/goods_list?cid=" + item.cat_id
        })
      },
      goToSearch() {
        // console.log('父组件点击....')
        uni.navigateTo({
          url: "/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss">
  // 左侧滑动区样式
  .scroll-container {
    display: flex;
    justify-content: space-around;
  }

  .scroll-left {
    width: 25%;
  }

  .scroll-right {
    width: 75%;
  }

  // 左侧滑动项样式
  .lfet-scroll-item {
    line-height: 60px;
    background-color: #f6f6f6;
    text-align: center;

    &.active {
      background-color: #fff;
      position: relative;

      &::before {
        content: ' ';
        display: block;
        width: 3px;
        height: 30px;
        background-color: #c00000;
        position: absolute;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
      }
    }
  }

  // 右侧滑动区样式

  // 标题样式
  .cate-level2-title {
    padding: 13px 0;
    font-size: 15px;
    font-weight: bold;
    text-align: center;
  }

  // 数据图片样式
  .cate-level3 {
    display: flex;
    flex-wrap: wrap;

    .cate-level3-item {
      width: 33.33%;
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    image {
      width: 60px;
      height: 60px;
    }

    text {
      font-size: 12px;
    }
  }
</style>
